<template>
    <el-card class="w-full pt-[5px] mt-[10px]">
        <div class="mb-[20px]">
            <el-button @click="submit">
                提交
            </el-button>
        </div>
        <el-table :data="tableData" @selection-change="handleSelectionChange" class="w-full" stripe>
            <el-table-column type="selection" width="55" />
            <el-table-column prop="productName" label="成果名称">
            </el-table-column>
            <el-table-column prop="reviewsChemeName" label="申报计划名称" />
            <el-table-column prop="applyDate" label="申报日期">
                <template #default="scope"> {{
                dateFormatter3(scope.row.applyDate) }} </template>
            </el-table-column>
            <el-table-column label="所有完成人">
                <template #default="scope">
                    <span v-for="(item, index) in scope.row.authorList1" :key="item.id">
                        <el-link type="primary" v-if="item.id != '3'" @click="openpeopledetails(item)">{{
                item.name
            }}<span v-if="index + 1 != scope.row.authorList1.length">,</span></el-link>
                        <span type="primary" v-else>{{
                item.name
            }}<span v-if="index + 1 != scope.row.authorList1.length">,</span></span>
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="honorState" label="获奖状态">
                <template #default="scope">
                    <Feedback :id="scope.row.honorState" :list="hjztist"></Feedback>
                </template>
            </el-table-column>

            <el-table-column prop="reviewStatus" label="评审状态" :formatter="formatpsztId" />
            <el-table-column prop="reviewResult" label="评审结果" :formatter="formatpsjgId" />
            <el-table-column prop="reviewMark" label="评审分值" />
            <el-table-column fixed="right" label="操作" width="200" align="center">
                <template #default="scope">
                    <el-button type="primary" v-if="scope.row.reviewStatus == 'submit'" size="small"
                        @click="view(scope.row)" plain>
                        查看评审
                    </el-button>
                    <el-button type="primary" v-if="scope.row.reviewStatus == 'notReview'" size="small"
                        @click="gethonorApplyBookReviewResultByExpertMet(scope.row)" plain>
                        评审
                    </el-button>
                    <el-button type="danger" v-if="scope.row.reviewStatus == 'notReview' && !scope.row.reviewMark"
                        size="small" @click="jpMet(scope.row)" plain>
                        拒评
                    </el-button>
                    <el-button type="primary" v-if="scope.row.reviewStatus == 'notReview' && scope.row.reviewMark"
                        size="small" @click="submitMet(scope.row)" plain>
                        提交
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    <reviewModal v-model="reviewModalVisible" ref="reviewModalRef" @callBack="callBackFun"></reviewModal>
    <reviewModalView v-model="reviewModalViewVisible" ref="reviewModalViewRef"></reviewModalView>
    <evaluationReject v-model="evaluationRejectVisible" :id="idJp" @callBack="callBackFun"></evaluationReject>
    <peopleDetails :activ="activ" :obj="obj" @close-dialog="activ = false"></peopleDetails>
</template>
<script setup>
import { gethonorApplyBookListByExpert, gethonorApplyBookReviewResultByExpert, reviewNoSubmitByExpert } from "@/api/achievement/cghj"
import useCategoryStore from "@/store/modules/category";
import reviewModal from "./reviewModal.vue"
import reviewModalView from "./reviewModalView.vue"
import { ElMessage, ElMessageBox } from "element-plus";
import evaluationReject from "./evaluationReject.vue"
import peopleDetails from '@/components/peopleDetails/peopleDetails.vue'

const reviewModalVisible = ref(false)
const reviewModalViewVisible = ref(false)
const evaluationRejectVisible = ref(false)


const categoryStore = useCategoryStore();
const { hjztist } = storeToRefs(categoryStore); // 解构状态
const { gethjztist } = categoryStore; // 解构方法

const tableData = ref([]);

const psztList = ref([{
    code: "notReview",
    name: '未评审'
}, {
    code: "submit",
    name: '已提交'
}, {
    code: "refuseReview",
    name: '拒评'
}])
const psjgList = ref([{
    code: "noPass",
    name: '不建议通过'
}, {
    code: "pass",
    name: '建议通过'
}])
const formatpsztId = (row, column, cellValue, index) => {
    if (psztList.value.length && cellValue) {
        let zcTips = psztList.value.find(item => item.code == cellValue) ? psztList.value.find(item => item.code == cellValue).name : ''
        if (zcTips == '未评审') {
            if (row.reviewMark) {
                return '暂存'
            } else {
                return zcTips
            }
        } else {
            return zcTips
        }
    } else {
        return ''
    }
}


const formatpsjgId = (row, column, cellValue, index) => {
    if (psjgList.value.length && cellValue) {
        return psjgList.value.find(item => item.code == cellValue) ? psjgList.value.find(item => item.code == cellValue).name : ''
    } else {
        return ''
    }
}

const getList = () => {
    let parms = {
        reviewStatus: 0
    }
    gethonorApplyBookListByExpert(parms).then(res => {
        tableData.value = res.data;
        tableData.value.forEach(item => {
            if (item.authornames && item.authorpids) {
                let zcNameArr = item.authornames.split(',')
                let zcNameArr1 = item.authorpids.split(',')
                let zcNameArr2 = zcNameArr.map((val, index) => {
                    return {
                        name: val,
                        id: zcNameArr1[index]
                    }
                })
                Reflect.set(item, 'authorList1', zcNameArr2)
            }
        })
    })
}

const callBackFun = () => {
    getList()
}
const reviewModalRef = ref(null)
const reviewModalViewRef = ref(null)
const gethonorApplyBookReviewResultByExpertMet = (i) => {
    let parms = {
        honorApplyBookId: i.id
    }
    gethonorApplyBookReviewResultByExpert(parms).then(res => {
        if (res.code == 200) {
            reviewModalVisible.value = true;
            reviewModalRef.value.getRuleForm(res.data)
        }
    })
}

const view = (i) => {
    let parms = {
        honorApplyBookId: i.id
    }
    gethonorApplyBookReviewResultByExpert(parms).then(res => {
        if (res.code == 200) {
            reviewModalViewVisible.value = true;
            reviewModalViewRef.value.getRuleForm(res.data)
        }
    })
}

const idJp = ref('')
const jpMet = (i) => {
    idJp.value = i.id
    evaluationRejectVisible.value = true
}

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

const submit = () => {
    let zcArr = multipleSelection.value.filter(item => item.reviewStatus == 'notReview' && item.reviewMark)
    if (zcArr && zcArr.length) {
        let zcArr1 = zcArr.map(val => val.id)
        let parms = {
            reviewObjectIds: zcArr1.join(',')
        }
        reviewNoSubmitByExpert(parms).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: '提交成功',
                    type: 'success'
                })
                getList()
            }
        })
    } else {
        ElMessage({
            message: '请选择未评审的记录并填写评审意见后再提交',
            type: 'warning'
        })
    }
}

const submitMet = (i) => {
    let parms = {
        reviewObjectIds: i.id
    }
    reviewNoSubmitByExpert(parms).then(res => {
        if (res.code == 200) {
            ElMessage({
                message: '提交成功',
                type: 'success'
            })
            getList()
        }
    })
}

///查看详情
const activ = ref(false)
const obj = ref('')
const openpeopledetails = function (row) {
    console.log(row)

    obj.value = row.id
    activ.value = true
}



if (!hjztist.value.length) {
    gethjztist()
}
getList()
</script>